#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 定义员工结构体
typedef struct {
    int employeeId;
    char name[50];
    char phone[11];
    char address[100];
} Employee;

// 定义日程记录结构体
typedef struct {
    int employeeId;
    char remark[100];
    time_t timestamp;
} ScheduleRecord;

// 创建日程表文件
void createScheduleTableFile() {
    FILE* file = fopen("schedule.txt", "w");
    fclose(file);
}

// 向日程表中添加记录
void addRecordToScheduleTable(char date[], ScheduleRecord record) {
    FILE* file = fopen("schedule.txt", "a");
    fprintf(file, "%s %d %s %ld\n", date, record.employeeId, record.remark, record.timestamp);
    fclose(file);
}

// 修改日程表中的记录
void modifyRecordInScheduleTable(char date[], int index, ScheduleRecord newRecord) {
    FILE* file = fopen("schedule.txt", "r");
    char line[100];
    int count = 0;
    FILE* tempFile = fopen("schedule_temp.txt", "w");

    // 逐行读取原文件的内容
    while (fgets(line, sizeof(line), file)) {
        if (count == index) {
            // 如果是需要修改的记录，写入新的记录
            fprintf(tempFile, "%s %d %s %ld\n", date, newRecord.employeeId, newRecord.remark, newRecord.timestamp);
        }
        else {
            // 否则，将原记录写入临时文件
            fprintf(tempFile, "%s", line);
        }
        count++;
    }

    fclose(file);
    fclose(tempFile);
    remove("schedule.txt");
    rename("schedule_temp.txt", "schedule.txt");
}

// 从日程表中删除记录
void deleteRecordFromScheduleTable(char date[], int index) {
    FILE* file = fopen("schedule.txt", "r");
    char line[100];
    int count = 0;
    FILE* tempFile = fopen("schedule_temp.txt", "w");

    while (fgets(line, sizeof(line), file)) {
        // 不是需要删除的记录，写入临时文件
        if (count != index) {
            fprintf(tempFile, "%s", line);
        }
        count++;
    }

    fclose(file);
    fclose(tempFile);
    remove("schedule.txt");
    rename("schedule_temp.txt", "schedule.txt");
}

// 显示日程表中的记录
void displayScheduleTable() {
    FILE* file = fopen("schedule.txt", "r");
    char line[100];

    while (fgets(line, sizeof(line), file)) {
        char date[11];
        int employeeId;
        char remark[100];
        time_t timestamp;

        sscanf(line, "%s %d %s %ld", date, &employeeId, remark, &timestamp);

        printf("Date: %s\n", date);
        printf("Employee ID: %d\n", employeeId);
        printf("Remark: %s\n", remark);
        printf("Timestamp: %s", asctime(gmtime(&timestamp)));
        printf("---------------------------\n");
    }

    fclose(file);
}

// 判断日程表是否可用
int isSchedulable() {
    FILE* file = fopen("schedule.txt", "r");
    char line[100];
    int employeeCount[100] = { 0 };

    while (fgets(line, sizeof(line), file)) {
        int employeeId;
        sscanf(line, "%*s %d", &employeeId);
        employeeCount[employeeId - 1]++;
    }

    fclose(file);

    for (int i = 0; i < 100; i++) {
        if (employeeCount[i] == 0) {
            return i + 1;
        }
    }

    return 0;
}

int main() {
    int choice;
    char date[11];
    ScheduleRecord record;
    int index;
    int employeeId;

    createScheduleTableFile();

    do {
        printf("1. 添加记录\n");
        printf("2. 修改记录\n");
        printf("3. 删除记录\n");
        printf("4. 显示日程表\n");
        printf("5. 检查是否可调度\n");
        printf("6. 退出\n");
        printf("请输入您的选择: ");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            printf("请输入日期（YYYY-MM-DD）：");
            scanf("%s", date);
            printf("请输入员工 ID：");
            scanf("%d", &record.employeeId);
            printf("请输入备注：");
            scanf("%s", record.remark);
            record.timestamp = time(NULL);

            addRecordToScheduleTable(date, record);
            break;

        case 2:
            printf("请输入日期（YYYY-MM-DD）：");
            scanf("%s", date);
            printf("请输入要修改的记录索引：");
            scanf("%d", &index);
            printf("请输入员工 ID：");
            scanf("%d", &record.employeeId);
            printf("请输入备注：");
            scanf("%s", record.remark);
            record.timestamp = time(NULL);

            modifyRecordInScheduleTable(date, index, record);
            break;

        case 3:
            printf("请输入日期（YYYY-MM-DD）：");
            scanf("%s", date);
            printf("请输入要删除的记录索引：");
            scanf("%d", &index);

            deleteRecordFromScheduleTable(date, index);
            break;

        case 4:
            displayScheduleTable();
            break;

        case 5:
            employeeId = isSchedulable();
            if (employeeId == 0) {
                printf("系统可调度。\n");
            }
            else {
                printf("无可用员工在第 %d 天进行调度。\n", employeeId);
            }
            break;

        case 6:
            printf("正在退出程序...\n");
            break;

        default:
            printf("无效的选择，请重试。\n");
        }

        printf("\n");
    } while (choice != 6);

    return 0;
}